class FishingSpot {
  final int id;
  final String name;
  final String description;
  final List<String> images;
  final String address;
  final double latitude;
  final double longitude;
  final double distance; // 距离用户的距离（公里）
  final double rating;
  final int reviewCount;
  final String priceRange;
  final List<String> fishTypes;
  final List<int> facilityIds;
  final String openHours;
  final String phone;
  final bool isOpen;
  final bool hasParking;
  final bool hasRestroom;
  final bool hasRestaurant;
  final bool allowsNightFishing;
  final String difficulty; // 'beginner', 'intermediate', 'advanced'

  FishingSpot({
    required this.id,
    required this.name,
    required this.description,
    required this.images,
    required this.address,
    required this.latitude,
    required this.longitude,
    required this.distance,
    required this.rating,
    required this.reviewCount,
    required this.priceRange,
    required this.fishTypes,
    required this.facilityIds,
    required this.openHours,
    required this.phone,
    required this.isOpen,
    required this.hasParking,
    required this.hasRestroom,
    required this.hasRestaurant,
    required this.allowsNightFishing,
    required this.difficulty,
  });

  factory FishingSpot.fromJson(Map<String, dynamic> json) {
    // 处理 images 字段，可能是对象数组或字符串数组
    List<String> imageUrls = [];
    if (json['images'] != null) {
      final images = json['images'] as List;
      for (var image in images) {
        if (image is String) {
          imageUrls.add(image);
        } else if (image is Map<String, dynamic> && image['imageUrl'] != null) {
          imageUrls.add(image['imageUrl']);
        }
      }
    }

    // 处理 fishTypes 字段，可能是字符串或数组
    List<String> fishTypesList = [];
    if (json['fishTypes'] != null) {
      if (json['fishTypes'] is String) {
        // 如果是字符串，按逗号分割
        final fishTypesStr = json['fishTypes'] as String;
        if (fishTypesStr.isNotEmpty) {
          fishTypesList = fishTypesStr.split(',').map((e) => e.trim()).toList();
        }
      } else if (json['fishTypes'] is List) {
        fishTypesList = List<String>.from(json['fishTypes']);
      }
    }

    return FishingSpot(
      id: json['id'],
      name: json['name'] ?? '',
      description: json['description'] ?? '',
      images: imageUrls,
      address: json['address'] ?? '',
      latitude: json['latitude']?.toDouble() ?? 0.0,
      longitude: json['longitude']?.toDouble() ?? 0.0,
      distance: json['distance']?.toDouble() ?? 0.0,
      rating: json['rating']?.toDouble() ?? 0.0,
      reviewCount: json['reviewCount'] ?? 0,
      priceRange: json['priceRange'] != null && json['priceRange'].toString().isNotEmpty ? json['priceRange'].toString() : '价格面议',
      fishTypes: fishTypesList,
      facilityIds: _convertFacilitiesToIds(json['facilities']),
      openHours: json['openHours'] ?? '营业时间待定',
      phone: json['phone'] ?? '',
      isOpen: json['isOpen'] ?? true,
      hasParking: json['hasParking'] ?? false,
      hasRestroom: json['hasRestroom'] ?? false,
      hasRestaurant: json['hasRestaurant'] ?? false,
      allowsNightFishing: json['allowsNightFishing'] ?? false,
      difficulty: json['difficulty'] ?? 'beginner',
    );
  }

  // 将设施名称转换为设施ID
  static List<int> _convertFacilitiesToIds(dynamic facilities) {
    if (facilities == null) return [];
    
    List<String> facilityNames = [];
    if (facilities is List) {
      facilityNames = List<String>.from(facilities);
    } else if (facilities is String) {
      facilityNames = [facilities];
    }
    
    // 设施名称到ID的映射
    const Map<String, int> facilityNameToId = {
      '洗手间': 1,
      '停车场': 2,
      '餐厅': 3,
      '小卖铺': 4,
      '自动售货机': 5,
      '指导钓鱼服务': 6,
      '休息区': 7,
      '烧烤区': 8,
      '儿童游乐设施': 9,
      '麻将桌': 10,
      '露营地': 11,
    };
    
    return facilityNames
        .map((name) => facilityNameToId[name])
        .where((id) => id != null)
        .cast<int>()
        .toList();
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'images': images,
      'address': address,
      'latitude': latitude,
      'longitude': longitude,
      'distance': distance,
      'rating': rating,
      'reviewCount': reviewCount,
      'priceRange': priceRange,
      'fishTypes': fishTypes,
      'facilityIds': facilityIds,
      'openHours': openHours,
      'phone': phone,
      'isOpen': isOpen,
      'hasParking': hasParking,
      'hasRestroom': hasRestroom,
      'hasRestaurant': hasRestaurant,
      'allowsNightFishing': allowsNightFishing,
      'difficulty': difficulty,
    };
  }
}